import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

import java.util.List;
/**
 * Write a description of class Cartman here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cartman extends Actor
{   private int vidas;
    private int salud;
    private GreenfootImage[] imag=new GreenfootImage[4];
    private int mov;
    private int dir;
    private int[] mochila;
    private int topemochila;
    private Arma[] arm;
    private GreenfootSound[] sound;
    private int disparo=0;
    
    /**
     * Act - do whatever the Cartman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public Cartman()
    {
        vidas=3;
        salud=100;
        mochila=new int[10];
        topemochila=0;
        arm=new Arma[2];
        imag[0] = new GreenfootImage("caa.png");
        imag[1] = new GreenfootImage("caa2.png");
        imag[2] = new GreenfootImage("caa2.png");
        imag[3] = new GreenfootImage("caa3.png");
        
        imag[1].mirrorVertically();
        imag[1].mirrorHorizontally();
        imag[2].mirrorHorizontally();
        setImage(imag[0]);
        mov=0;
        dir=0;
        sound=new GreenfootSound[4];
        sound[0]=new GreenfootSound("this is bull crap.mp3");
        sound[1]=new GreenfootSound("aufkm.mp3");
        sound[2]=new GreenfootSound("what the fuck now.mp3");
        sound[3]=new GreenfootSound("lets do it .mp3");
        arm[0]=new Cuchillo();
        arm[1]=new Pistola();
       
    }
    public void act() 
    {   
       Actor aux;
       Actor a;
       Letrero l1;
       World w=getWorld();
       
       w.addObject(arm[1],getX(),getY());
       setImage(getImage());
        if(Greenfoot.isKeyDown("right")&& (aux=getOneObjectAtOffset(50,0,cerca.class))==null){
        setRotation(0);
        dir=1;
        setImage(imag[2]);
        if(mov==0)
        setRotation(0);
        if(mov==3)
        setRotation(10);
        if(mov==6)
        setRotation(-10);
        
        if(mov==9)
         mov=0;
        
        move(2);
        
        mov++;
        
        
        
       }    
       if(Greenfoot.isKeyDown("left")&& (aux=getOneObjectAtOffset(-50,0,cerca.class))==null){
        setRotation(180);
        dir=2;
        setImage(imag[1]);
        if(mov==0)
        setRotation(180);
        if(mov==3)
        setRotation(190);
        if(mov==6)
        setRotation(170);
        
        if(mov==9)
        mov=0;
        
        mov++;
        move(2);
        }
       if(Greenfoot.isKeyDown("up")&& (aux=getOneObjectAtOffset(0,0,cerca.class))==null){
        setRotation(-90);
        dir=3;
        setImage(imag[3]);
        if(mov==0)
        setRotation(-90);
        if(mov==3)
        setRotation(-100);
        if(mov==6)
        setRotation(-80);
        
        if(mov==9)
        mov=0;
        move(1);
        mov++;
       }
        if(Greenfoot.isKeyDown("down")){
        setRotation(90);
        dir=0;
        setImage(imag[0]);
        if(mov==0)
        setRotation(90);
        if(mov==3)
        setRotation(100);
        if(mov==6)
        setRotation(80);
        
        if(mov==9)
        mov=0;
        
        move(1);
        mov++;
        
       }
       
       if((a=getOneObjectAtOffset(50,0,Casa1.class))!=null){
           if(Greenfoot.isKeyDown("enter")){
               if(((Casa1)a).getEstado()==false){
               int i=Greenfoot.getRandomNumber(3);
               sound[i].play();
               l1=new Letrero("Cerrado",1);
               w.addObject(l1,400,400);
               Greenfoot.delay(20);
            }
            }
        }
        
         if((a=getOneObjectAtOffset(50,0,Casa1.class))!=null){
           if(Greenfoot.isKeyDown("enter")){
               if(((Casa1)a).getEstado()==true){
               Greenfoot.setWorld(new sub1(this));
            }
            }
        }
         if((a=getOneObjectAtOffset(50,0,casaCartman.class))!=null){
           if(Greenfoot.isKeyDown("enter")){
               
               Greenfoot.delay(20);
               sound[3].play();
               l1=new Letrero("Es mi casa,no queda nada dentro",1);
               w.addObject(l1,400,400);
              
            
            }
        }
        
        if((a=getOneObjectAtOffset(5,0,Caja.class))!=null){
          if(Greenfoot.isKeyDown("enter")){
           if(((Caja)a).getContenido()==0 )
            sound[0].play();
           
           
           if(((Caja)a).getContenido()==2 )
                 setArma(1);
                
           
           if(((Caja)a).getContenido()==5 ){
               sound[3].play();
               l1=new Letrero("Es una llave la guardaré en mi mochila",1);
               w.addObject(l1,400,400);
               if(insertaEnMochila(5)==true)
                 w.removeObject(a);
            
            }
           w.removeObject(a);
          }
          
        }
        
        if(Greenfoot.isKeyDown("space")){
             disparo=1;
        }
       
   }
   
   public int getDir(){
     return dir;
    }
    
   
    
   public void setLocation(int xx,int yy){
    
        int x2 = getX()-xx;//gets where the man is trying to move
        int y2 = getY()-yy;
        
        List<Otros> everyone = getWorld().getObjects(Otros.class);
        for(int g = 0;g < everyone.size();g++)
            {
                Actor one = everyone.get(g);
                one.setLocation(one.getX()+x2,one.getY()+y2);//sets the location of the other actors back
            }
         
       // ((Ciudad) getWorld()).setThing(x2, y2);//moves the world back
    }
    
    public int getDisparo(){
      return disparo;
    }
    
    public void setDisparo(){
      disparo=0; 
    }
    
    public void setArma(int t){
     arm[1].borrar();
     
      if(t==0){
      arm[1]=new Pistola();
      getWorld().addObject(arm[1],getX(),getY());
     }
     if(t==1){
      arm[1]=new Ametralladora();
      getWorld().addObject(arm[1],getX(),getY());
     }
     if(t==2){
      arm[1]=new Escopeta();
      getWorld().addObject(arm[1],getX(),getY());
     }
    }
    public boolean insertaEnMochila(int ob){
      if(topemochila!=9){
          mochila[topemochila]=ob;
          topemochila++;
          return true;
        }
      else {
       Letrero ln=new Letrero("Mochila llena",1);
       getWorld().addObject(ln,400,400);
       return false;
        }
    }
}

